package com.leyou.search.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.search.constants.SearchConstants;
import com.leyou.search.dao.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.repository.GoodsRepository;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.nested.Nested;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.*;
import java.util.stream.Collectors;

import static com.leyou.search.constants.SearchConstants.*;


/**
 * @author 虎哥
 */
@Service
@Slf4j
public class SearchServiceImpl implements SearchService {

    private final GoodsRepository goodsRepository;

    private final ItemClient itemClient;

    public SearchServiceImpl(GoodsRepository goodsRepository, ItemClient itemClient) {
        this.goodsRepository = goodsRepository;
        this.itemClient = itemClient;
    }
    
    @Override
    public void createIndexAndMapping() {
        // 删除已经存在的索引库
        try {
            goodsRepository.deleteIndex();
        } catch (Exception e) {
            log.error("删除失败，可能索引库不存在！", e);
        }
        // 然后创建一个新的
        goodsRepository.createIndex("{\n" +
                "  \"settings\": {\n" +
                "    \"analysis\": {\n" +
                "      \"analyzer\": {\n" +
                "        \"my_pinyin\": {\n" +
                "          \"tokenizer\": \"ik_smart\",\n" +
                "          \"filter\": [\n" +
                "            \"py\"\n" +
                "          ]\n" +
                "        }\n" +
                "      },\n" +
                "      \"filter\": {\n" +
                "        \"py\": {\n" +
                "\t\t  \"type\": \"pinyin\",\n" +
                "          \"keep_full_pinyin\": true,\n" +
                "          \"keep_joined_full_pinyin\": true,\n" +
                "          \"keep_original\": true,\n" +
                "          \"limit_first_letter_length\": 16,\n" +
                "          \"remove_duplicated_term\": true\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  },\n" +
                "  \"mappings\": {\n" +
                "    \"properties\": {\n" +
                "      \"id\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"suggestion\": {\n" +
                "        \"type\": \"completion\",\n" +
                "        \"analyzer\": \"my_pinyin\",\n" +
                "        \"search_analyzer\": \"ik_smart\"\n" +
                "      },\n" +
                "      \"title\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"analyzer\": \"my_pinyin\",\n" +
                "        \"search_analyzer\": \"ik_smart\"\n" +
                "      },\n" +
                "      \"image\":{\n" +
                "        \"type\": \"keyword\",\n" +
                "        \"index\": false\n" +
                "      },\n" +
                "      \"updateTime\":{\n" +
                "        \"type\": \"date\"\n" +
                "      },\n" +
                "      \"specs\":{\n" +
                "        \"type\": \"nested\",\n" +
                "        \"properties\": {\n" +
                "          \"name\":{\"type\": \"keyword\" },\n" +
                "          \"value\":{\"type\": \"keyword\" }\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}");
    }

    @Override
    public void loadData() {
        int page = 1, rows = 100;
        while (true) {
            // 分页查询已经上架的spu
            PageDTO<SpuDTO> result = itemClient.querySpuByPage(page, rows, true, null, null, null);
            List<SpuDTO> list = result.getItems();

            // 遍历Spu集合，把SpuDTO通过buildGoods方法转为Goods
            List<Goods> goodsList = list.stream().map(this::buildGoods).collect(Collectors.toList());

            // 批量写入Elasticsearch
            goodsRepository.saveAll(goodsList);

            // 翻页
            page++;

            // 获取总页数
            Long totalPage = result.getTotalPage();
            // 判断是否还有spu没有查询
            if(page > totalPage){
                // 没有则结束
                break;
            }
        }
    }

    private Goods buildGoods(SpuDTO spu) {
        // 1.自动补全的提示字段
        List<String> suggestion = new ArrayList<>(
                Arrays.asList(StringUtils.split(spu.getCategoryName(), "/")));
        suggestion.add(spu.getName());
        suggestion.add(spu.getBrandName());

        // 2.sku的价格集合
        // 2.1.查询sku集合
        List<SkuDTO> skuList = spu.getSkus();
        if (CollectionUtils.isEmpty(skuList)) {
            // 没有sku，我们去查询
            skuList = itemClient.querySkuBySpuId(spu.getId());
        }
        // 2.2.获取价格集合
        Set<Long> prices = skuList.stream().map(SkuDTO::getPrice).collect(Collectors.toSet());

        // 3.商品销量
        long sold = skuList.stream().mapToLong(SkuDTO::getSold).sum();

        // 4.sku的某个图片
        String image = StringUtils.substringBefore(skuList.get(0).getImages(), ",");

        // 5.规格参数
        List<Map<String, Object>> specs = new ArrayList<>();
        // 5.1.查询规格参数name和value键值对，只查询参与搜索的
        List<SpecParamDTO> params = itemClient.querySpecsValues(spu.getId(), true);
        // 5.2.封装
        for (SpecParamDTO param : params) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("name", param.getName());
            map.put("value", chooseSegment(param));
            specs.add(map);
        }

        // 创建Goods对象，并封装数据
        Goods goods = new Goods();
        goods.setUpdateTime(new Date());
        // 自动补全的提示字段
        goods.setSuggestion(suggestion);
        // 规格参数
        goods.setSpecs(specs);
        // 商品销量
        goods.setSold(sold);
        // 商品标题
        goods.setTitle(spu.getTitle() + StringUtils.join(suggestion, " "));
        // sku的价格集合
        goods.setPrices(prices);
        // sku的某个图片
        goods.setImage(image);
        goods.setCategoryId(spu.getCid3());
        goods.setBrandId(spu.getBrandId());
        goods.setId(spu.getId());
        return goods;
    }

    private Object chooseSegment(SpecParamDTO p) {
        Object value = p.getValue();
        if (value == null || StringUtils.isBlank(value.toString())) {
            return "其它";
        }
        if(!p.getNumeric() || StringUtils.isBlank(p.getSegments()) || value instanceof Collection){
            return value;
        }
        double val = parseDouble(value.toString());
        String result = "其它";
        // 保存数值段
        for (String segment : p.getSegments().split(",")) {
            String[] segs = segment.split("-");
            // 获取数值范围
            double begin = parseDouble(segs[0]);
            double end = Double.MAX_VALUE;
            if (segs.length == 2) {
                end = parseDouble(segs[1]);
            }
            // 判断是否在范围内
            if (val >= begin && val < end) {
                if (segs.length == 1) {
                    result = segs[0] + p.getUnit() + "以上";
                } else if (begin == 0) {
                    result = segs[1] + p.getUnit() + "以下";
                } else {
                    result = segment + p.getUnit();
                }
                break;
            }
        }
        return result;
    }

    private double parseDouble(String str) {
        try {
            return Double.parseDouble(str);
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public Mono<List<String>> getSuggestion(String keyPrefix) {
        if (StringUtils.isBlank(keyPrefix)) {
            throw new LyException(400, "请求参数不能为空！");
        }
        return goodsRepository.suggestBySingleField(SUGGESTION_FIELD, keyPrefix);
    }

    @Override
    public Mono<PageInfo<Goods>> searchGoods(SearchParamDTO param) {
        // 1.构建查询条件的工厂对象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 1.1.source过滤
        sourceBuilder.fetchSource(DEFAULT_SOURCE_FIELD, new String[0]);
        // 1.2.查询条件
        String key = param.getKey();
        if(StringUtils.isBlank(key)){
            // 搜索条件为null，返回异常
            throw new LyException(400, "搜索条件不能为空！");
        }
        sourceBuilder.query(QueryBuilders.matchQuery(DEFAULT_SEARCH_FIELD, key));
        // 1.3.分页条件
        sourceBuilder.from(param.getFrom());
        sourceBuilder.size(param.getSize());
        // 1.4.排序条件
        if(StringUtils.isNotBlank(param.getSortBy())){
            // 排序字段存在，才去排序
            sourceBuilder.sort(param.getSortBy(), param.getDesc() ? SortOrder.DESC: SortOrder.ASC);
        }
        // 1.5.高亮条件
        sourceBuilder.highlighter(new HighlightBuilder().field(DEFAULT_SEARCH_FIELD)
                .preTags(DEFAULT_PRE_TAG).postTags(DEFAULT_POST_TAG));

        // 2.查询
        return goodsRepository.queryBySourceBuilderForPageHighlight(sourceBuilder);
    }


    @Override
    public Mono<Map<String, List<?>>> getFilters(SearchParamDTO param) {
        // 1.构建查询条件的工厂对象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 1.1.查询条件
        String key = param.getKey();
        if (StringUtils.isBlank(key)) {
            // 搜索条件为null，返回异常
            throw new LyException(400, "搜索条件不能为空！");
        }
        sourceBuilder.query(QueryBuilders.matchQuery(SearchConstants.DEFAULT_SEARCH_FIELD, key));

        // 1.2.分页设置为0，不需要商品数据，只要聚合结果
        sourceBuilder.size(0);

        // 2.添加聚合条件
        // 2.1.分类的聚合
        sourceBuilder.aggregation(AggregationBuilders.terms("categoryAgg").field(CATEGORY_FIELD_NAME));
        // 2.2.品牌的聚合
        sourceBuilder.aggregation(AggregationBuilders.terms("brandAgg").field(BRAND_FIELD_NAME));
        // 2.3.规格参数nested聚合
        sourceBuilder.aggregation(
                // nested聚合
                AggregationBuilders.nested("specAgg", SPEC_FIELD_PATH)
                        // 嵌套名称的聚合
                        .subAggregation(AggregationBuilders.terms("nameAgg").field(SPEC_NAME_FIELD_NAME)
                                // 嵌套对value的聚合
                                .subAggregation(AggregationBuilders.terms("valueAgg").field(SPEC_VALUE_FIELD_NAME))
                        )
        );
        // 3.查询
        Mono<Aggregations> mono = goodsRepository.aggregationBySourceBuilder(sourceBuilder);

        // 4.解析结果
        return mono.map(this::parseAggregationsResult);
    }

    private Map<String, List<?>> parseAggregationsResult(Aggregations aggregations){
        // 1.创建一个Map结果
        Map<String, List<?>> filters = new LinkedHashMap<>();

        // 2.解析分类聚合结果
        // 2.1.根据名称获取聚合结果
        Terms categoryAgg = aggregations.get("categoryAgg");
        // 2.2.获取buckets,从buckets中解析出分类id的集合
        List<Long> categoryIds = categoryAgg.getBuckets()
                .stream()
                .map(Terms.Bucket::getKeyAsNumber)
                .map(Number::longValue)
                .collect(Collectors.toList());
        // 2.3.根据分类的id查询分类的对象
        List<CategoryDTO> categories = itemClient.queryCategoryByIds(categoryIds);
        // 2.4.存入filters
        filters.put("分类", categories);

        // 3.解析品牌聚合结果
        // 3.1.根据名称获取聚合结果
        Terms brandAgg = aggregations.get("brandAgg");
        // 3.2.获取buckets,从buckets中解析出分类id的集合
        List<Long> brandIds = brandAgg.getBuckets()
                .stream()
                .map(Terms.Bucket::getKeyAsNumber)
                .map(Number::longValue)
                .collect(Collectors.toList());
        // 3.3.根据品牌的id查询品牌的对象
        List<BrandDTO> brands = itemClient.queryBrandByIds(brandIds);
        // 3.4.存入filters
        filters.put("品牌", brands);

        // 4.解析规格参数聚合结果
        // 4.1.根据名称获取聚合结果
        Nested specAgg = aggregations.get("specAgg");
        // 4.2.获取对规格参数名称的聚合结果
        Terms nameAgg = specAgg.getAggregations().get("nameAgg");
        // 4.3.获取buckets，并且遍历
        for (Terms.Bucket bucket : nameAgg.getBuckets()) {
            // 4.3.1.从bucket中获取key，就是规格参数的名称
            String specName = bucket.getKeyAsString();
            // 4.3.2.获取当前规格参数的值的聚合结果
            Terms valueAgg = bucket.getAggregations().get("valueAgg");
            // 4.3.3.从valueAgg中获取buckets，并解析其中的key
            List<String> values = valueAgg.getBuckets()
                    .stream()
                    .map(Terms.Bucket::getKeyAsString)
                    .collect(Collectors.toList());
            // 4.3.4.放入filters
            filters.put(specName, values);
        }
        return filters;
    }

    private QueryBuilder buildBasicQuery(SearchParamDTO param) {
        if (StringUtils.isBlank(param.getKey())) {
            throw new LyException(400, "请求参数不能为空！");
        }
        // 1.创建布尔查询
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        // 2.关键字查询， must
        queryBuilder.must(QueryBuilders.matchQuery(DEFAULT_SEARCH_FIELD, param.getKey()));
        // 3.过滤项查询， filter
        Map<String, String> filters = param.getFilters();
        // 判断是否有过滤项
        if(CollectionUtils.isEmpty(filters)){
            // 没有，直接返回
            return queryBuilder;
        }
        // 4.添加过滤项, {分类: 105, 品牌: 18374, CPU频率: "2.0-2.5GHz"}
        for (Map.Entry<String, String> entry : filters.entrySet()) {
            // 4.1.获取过滤项的key： 分类、品牌、CPU频率
            String key = entry.getKey();
            // 4.2.获取过滤项值：105、18374、2.0-2.5GHz
            String value = entry.getValue();
            // 4.3.添加过滤项
            if("分类".equals(key)){
                // 4.3.1.按照分类来处理
                queryBuilder.filter(QueryBuilders.termQuery(CATEGORY_FIELD_NAME, value));
            }else if("品牌".equals(key)){
                // 4.3.2.按照品牌来处理
                queryBuilder.filter(QueryBuilders.termQuery(BRAND_FIELD_NAME, value));
            } else {
                // 4.3.3.按照规格参数处理，nested类型
                // 1)创建布尔查询，组合name和value查询
                BoolQueryBuilder query = QueryBuilders.boolQuery();
                query.must(QueryBuilders.termQuery(SPEC_NAME_FIELD_NAME, key));
                query.must(QueryBuilders.termQuery(SPEC_VALUE_FIELD_NAME, value));
                // 2)添加到nested查询中
                queryBuilder.filter(QueryBuilders.nestedQuery(SPEC_FIELD_PATH, query, ScoreMode.None));
            }
        }
        return queryBuilder;
    }

    @Override
    public void saveGoodsById(Long spuId) {
        // 需要spu
        SpuDTO spu = itemClient.queryGoodsById(spuId);
        // 构建goods
        Goods goods = buildGoods(spu);
        // 写入索引库
        goodsRepository.save(goods);
    }

    @Override
    public void deleteGoodsById(Long spuId) {
        goodsRepository.deleteById(spuId);
    }

}